#ifndef file_afat_TAB_M_H
#define	file_afat_TAB_M_H

#include "stdint.h"
#include "rtctime.h"

#ifdef	__cplusplus
extern "C" {
#endif	

#define AFAT_NAME_SIZE        24

	typedef enum
	{
		FILE_AFAT_ERR_NONE = 0,
		FILE_AFAT_ERR_RENAME = 1,
		FILE_AFAT_ERR_PTAB = 2,
		FILE_AFAT_ERR_LEN = 3,
		FILE_AFAT_ERR_FULL = 4,
		FILE_AFAT_ERR_NO_FILE = 5,
		FILE_AFAT_ERR_NO_SITE = 6,
		FILE_AFAT_ERR_TAB_CRC = 7,
		FILE_AFAT_ERR_ADDR = 8,
		FILE_AFAT_ERR_OPT_MODE = 9,
		FILE_AFAT_ERR_HAL = 10,
	}file_afat_err_type;

	enum file_afat_mode_type
	{
		 FILE_AFAT_MODE_NONE    =   0,
		 FILE_AFAT_MODE_READ   =    1,
		 FILE_AFAT_MODE_WRITE   =   2,
		 FILE_AFAT_MODE_MODIFY  =   4,
	};
	
	#define FILE_AFAT_ATTR_EDIT       (1UL<<0)
	
	typedef struct
	{
		int32_t addr_base;
		int32_t  size;      
		int32_t  mem_alloc; 
		uint32_t attr;
		char name[AFAT_NAME_SIZE];
	} afat_tab_type;
	
	typedef struct
	{
		afat_tab_type *tab;					
		int16_t tab_len;					
		int32_t write_alain;				
		int32_t erase_alain;				
		int32_t mem_size;					
		
		void(*tab_save)(void);				
		void(*tab_read)(void);				
		
		uint8_t (*write)(uint32_t addr, int32_t len, void *pdat); 
		uint8_t (*read)(uint32_t addr, int32_t len, void *pdat); 
		uint8_t (*erase)(uint32_t addr);
		uint8_t (*flush)(void);					
	} file_afat_cg_type;
	
	struct file_afat_obj_str
	{
		int16_t act_file_amount;    
		int32_t new_file_addr;		
		int32_t erase_now_addr;		
		
		const file_afat_cg_type *cfg;
	};
	
	typedef struct file_afat_obj_str file_afat_obj_type;

	struct file_afat_file_str
	{
		int32_t seelk;						
		enum file_afat_mode_type opt_mode;	
		
		int16_t site_in_tab;				
		uint32_t wr_alain_real;				
		afat_tab_type info;					
		file_afat_obj_type *fat;
		
	};

	typedef struct file_afat_file_str file_afat_fil_type;
	
	file_afat_err_type afat_init(file_afat_obj_type *obj);

	file_afat_err_type afat_save(file_afat_obj_type *obj);

	file_afat_err_type afat_clr(file_afat_obj_type *obj);

	file_afat_err_type afat_file_open_rd(file_afat_obj_type *obj, file_afat_fil_type *fil, char *name);
	file_afat_err_type afat_file_seek(file_afat_fil_type *fil, int32_t site);
	file_afat_err_type afat_file_read(file_afat_fil_type *fil, void *dat, int32_t Len, int32_t * LenBack);
	
	file_afat_err_type afat_file_open_wr(file_afat_obj_type *obj, file_afat_fil_type *fil, char *name, uint32_t size); 
	file_afat_err_type afat_file_write(file_afat_fil_type *fil, void *dat, int32_t Len);
	file_afat_err_type afat_file_close(file_afat_fil_type *fil);
	
#ifdef	__cplusplus
}
#endif

#endif	
